/**
 *
 */
package org.example;

import java.util.List;

import org.example.util.SessionFactoryUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;


public class HQLTestOne {

    /**
     * HQL最大值函数max()/最小值min()
     */
    @Test
    public void testOne() {
        //获得一个Session
        Session session = SessionFactoryUtil.currentSession();
        //开启事物
        session.beginTransaction();
        //编写HQL:投影查询，查询单列
        String hql = " select min(u.salary) from org.example.pojo.User u";
        //创建Query对象
        Query query = session.createQuery(hql);
        //执行查询，获得结果，List<Object>只能是引用数据类型
        List<Double> list = query.list();
        //遍历
        for (Double d : list) {
            System.out.println(d);
        }
        SessionFactoryUtil.closeSession();
    }

    /**
     * HQL计数函数count()
     */
    @Test
    public void testTwo() {
        //获得一个Session
        Session session = SessionFactoryUtil.currentSession();
        //开启事物
        session.beginTransaction();
        //编写HQL:
        String hql = " select count(1) from org.example.pojo.User u";
        //创建Query对象
        Query query = session.createQuery(hql);
        //执行查询，获得结果
        List<Long> list = query.list();
        //遍历
        for (Long d : list) {
            System.out.println(d);
        }
        SessionFactoryUtil.closeSession();
    }

    /**
     * HQL求和函数sum()
     */
    @Test
    public void testThree() {
        //获得一个Session
        Session session = SessionFactoryUtil.currentSession();
        //开启事物
        session.beginTransaction();
        //编写HQL:
        String hql = " select sum(u.salary) from org.example.pojo.User u where u.sex='女'";
        //创建Query对象
        Query query = session.createQuery(hql);
        //执行查询，获得结果
        List<Double> list = query.list();
        //遍历
        for (Double d : list) {
            System.out.println(d);
        }
        SessionFactoryUtil.closeSession();
    }

    /**
     * HQL平均值函数avg()
     */
    @Test
    public void testFive() {
        //获得一个Session
        Session session = SessionFactoryUtil.currentSession();
        //开启事物
        session.beginTransaction();
        //编写HQL:
        String hql = " select avg(u.salary) from org.example.pojo.User u";
        //创建Query对象
        Query query = session.createQuery(hql);
        //执行查询，获得结果
        List<Double> list = query.list();
        //遍历
        for (Double d : list) {
            System.out.println(d);
        }
        SessionFactoryUtil.closeSession();
    }

    /**
     * HQL返回数据库当前日期:CURRENT_DATE()/当前时间：CURRENT_TIME() /当前日期时间：CURRENT_TIMESTAMP()
     */
    @Test
    public void testFour() {
        //获得一个Session
        Session session = SessionFactoryUtil.currentSession();
        //开启事物
        session.beginTransaction();
        //编写HQL:
        String hql = " select CURRENT_DATE(),CURRENT_TIME(),CURRENT_TIMESTAMP() from org.example.pojo.User u";
        //创建Query对象
        Query query = session.createQuery(hql);
        //执行查询，获得结果，多列查询，使用了Object接受结果
        List<Object[]> list = query.list();
        //遍历
        for (Object[] d : list) {
            System.out.println(d[0] + "   " + d[1] + "   " + d[2]);
        }
        SessionFactoryUtil.closeSession();
    }
}
